package net.wwang.demo.model;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * @BelongsProject: demo
 * @BelongsPackage: net.wwang.demo.model
 * @Author: WangWei
 * @CreateTime: 2020-05-28 14:13
 * @Description: 用户
 */
@Data
@Entity(name = "user")
@Table(name = "user")
public class User implements Serializable, UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Long id;
    @Column(name="username")
    private String username;
    @Column(name="password")
    private String password;
    @Column(name="tel")
    private String tel;
    @Column
    private String role;


    /**
     * @description: 账户是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * @description: 账户是否被冻结
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * @description: 账户密码是否过期（对于高密码安全性场景）
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * @description: 账号是否可用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
       List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        String[] roles = this.getRole().split(",");
        for (String role: roles
             ) {
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role));
        }
        return authorities;
    }
}
